package com.eastcom.providor.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.eastcom.bm.atom.Atom_MetalKpiBase;
import com.eastcom.providor.dao.IMetalKpiBaseDao;
import com.eastcom.providor.service.BaseService;
import com.eastcom.providor.service.IMetalKpiBaseService;

public class MetalKpiBaseService extends BaseService implements IMetalKpiBaseService {

	private IMetalKpiBaseDao metalKpiBaseDao;
	
	public IMetalKpiBaseDao getMetalKpiBaseDao() {
		return metalKpiBaseDao;
	}

	public void setMetalKpiBaseDao(IMetalKpiBaseDao metalKpiBaseDao) {
		this.metalKpiBaseDao = metalKpiBaseDao;
	}

	public boolean editMetalKpiBase(Atom_MetalKpiBase metalKpiBase) {
		try{
			metalKpiBaseDao.editMetalKpiBase(metalKpiBase);
			return true;
		}
		catch(Exception e){
			log.error("MetalKpiBaseService editMetalKpiBase error", e);
		}

		return false;
	}
	
	public boolean editMetalKpiBases(List<Atom_MetalKpiBase> metalKpiBases){
		try{
			metalKpiBaseDao.editMetalKpiBase(metalKpiBases);
			return true;
		}
		catch(Exception e){
			log.error("MetalKpiBaseService editMetalKpiBase error", e);
		}

		return false;
	}

	public Atom_MetalKpiBase findMetalKpiBaseById(String kpiId,boolean initialExpand){
        try{
        	Atom_MetalKpiBase kpibase= metalKpiBaseDao.findMetalKpiBaseById(kpiId, initialExpand);
        	List<Atom_MetalKpiBase> kpibases=new ArrayList<Atom_MetalKpiBase>();
        	initialRelatedKpiName(kpibases);
        	return kpibase;
		}
		catch(Exception e){
			log.error("MetalKpiBaseService findMetalKpiBaseById "+kpiId+" error", e);
		}

		return null;
	}

	public List<Atom_MetalKpiBase> selectAllKpiBase() {
		try{
        	return metalKpiBaseDao.selectAllKpiBase();
		}
		catch(Exception e){
			log.error("MetalKpiBaseService selectAllKpiBase error", e);
		}

		return null;
	}
	
	public List<Atom_MetalKpiBase> selectMetalKpiBaseByIds(List<String> kpiIds, boolean initialExpand) {
		try{
			List<Atom_MetalKpiBase> kpibases= metalKpiBaseDao.selectMetalKpiBaseByIds(kpiIds, initialExpand);
			initialRelatedKpiName(kpibases);
			return kpibases;
		}
		catch(Exception e){
			log.error("MetalKpiBaseService selectMetalKpiBaseByIds error", e);
		}

		return null;
	}
	
	public List<Atom_MetalKpiBase> selectMetalKpiBaseByBusinessKpiType(String businessType,String kpiType,String name,boolean initialExpand) {
		try{
			List<Atom_MetalKpiBase> kpibases= metalKpiBaseDao.selectMetalKpiBaseByBusinessKpiType(businessType,kpiType,name,initialExpand);
			initialRelatedKpiName(kpibases);
			return kpibases;
		}
		catch(Exception e){
			log.error("MetalKpiBaseService selectMetalKpiBaseByBusinessType "+businessType+" error", e);
		}

		return null;
	}
	
	public boolean saveMetalKpiBaseAlone(Atom_MetalKpiBase metalKpiBases){
		List <Atom_MetalKpiBase> list = new ArrayList<Atom_MetalKpiBase>();
		list.add(metalKpiBases);
        try{
        	metalKpiBaseDao.saveMetalKpiBase(list);
        	return true;
		}
		catch(Exception e){
			log.error("MetalKpiBaseService saveMetalKpiBase error", e);
		}

		return false;
	}
	
	public boolean saveMetalKpiBase(List<Atom_MetalKpiBase> metalKpiBases){
        try{
        	metalKpiBaseDao.saveMetalKpiBase(metalKpiBases);
        	return true;
		}
		catch(Exception e){
			log.error("MetalKpiBaseService saveMetalKpiBase error", e);
		}

		return false;
	}
	
	public boolean deleteMetalKpiBasesByListID(List<String> idList) {
		List<Atom_MetalKpiBase> metalKpiBases = new ArrayList<Atom_MetalKpiBase>();
		
		for(String id:idList){
			Atom_MetalKpiBase amk = new Atom_MetalKpiBase();
			amk.setId(id);
			metalKpiBases.add(amk);
		}
		try{
        	metalKpiBaseDao.deleteMetalKpiBases(metalKpiBases);
        	return true;
		}
		catch(Exception e){
			log.error("MetalKpiBaseService deleteMetalKpiBases error", e);
		}

		return false;
	}

	public boolean deleteMetalKpiBases(List<Atom_MetalKpiBase> metalKpiBases) {
		try{
        	metalKpiBaseDao.deleteMetalKpiBases(metalKpiBases);
        	return true;
		}
		catch(Exception e){
			log.error("MetalKpiBaseService deleteMetalKpiBases error", e);
		}

		return false;
	}

	public void initialRelatedKpiName(List<Atom_MetalKpiBase> kpiBases) throws Exception{
		if(kpiBases==null||kpiBases.isEmpty())
			return;
		
		Set<String> ids=new HashSet<String>();
		Map<Atom_MetalKpiBase,String[]> kpiBaseRelatedKpis=new HashMap<Atom_MetalKpiBase,String[]> ();
		for(Atom_MetalKpiBase kpBase:kpiBases){
			if(kpBase.getRelatedKpis()==null||"".equals(kpBase.getRelatedKpis().trim()))
				continue;
			
			String[] tempIds=kpBase.getRelatedKpis().split(",");
			kpiBaseRelatedKpis.put(kpBase, tempIds);
			for(String kpi:tempIds)
				ids.add(kpi);
		}
		
		if(kpiBaseRelatedKpis.isEmpty())
			return;
		
		Map<Object,Map> kpiNameMap=metalKpiBaseDao.getKpiNameById(ids);
		if(kpiNameMap==null||kpiNameMap.isEmpty())
			return;
		
		for(Atom_MetalKpiBase kpiBase:kpiBaseRelatedKpis.keySet()){
			String kpiIdName="";
			String[] relatedKpis=kpiBaseRelatedKpis.get(kpiBase);
			for(String kpi:relatedKpis){
				String name=kpiNameMap.get(kpi)==null||kpiNameMap.get(kpi).get("kpi_name_cn")==null?" ":(String)kpiNameMap.get(kpi).get("kpi_name_cn");
				kpiIdName+=kpi+"&"+name+"@#";
			}
			
			kpiBase.setRelatedKpis(kpiIdName);
		}
	}
	
}
